#!/bin/bash
# By jagadish manchala
# A Script To Find the Biggest of the Files

if [ "$1" = "" -o "$2" = "" ]
then
        echo
        echo "Usage: $0 FOLDER_NAME FILE_COUNT"
        echo
        echo "FOLDER_NAME: Folder to be searched for largest files (with full path)"
        echo "FILE_COUNT: Count of the largest files to fetch"
        echo
        echo "Example:  $0 /etc 5"
        echo

        exit 1
fi

SEARCH_DIR=$1
FILES_COUNT=$2

DISK_SIZE_BYTES=`df -PT -B 1 | awk '{if ($7 == "/")  print $3}'`

TEMP_FILE=/tmp/largest_files.tmp

printf "\n%-18s %-10s %-15s %-25s %s \n\n" "[SIZE (BYTES)]" "[% OF DISK]" "[OWNER]" "[LAST MODIFIED ON]" "[FILE]"

SUM=0

for FILE in `find $SEARCH_DIR -type f -exec du {} \; | sort -rn | head -$FILES_COUNT | awk '{print $2}'`
	do
                FILE_SIZE_BYTES=`stat --print %s $FILE`
				OWNER=`stat --printf %U $FILE`
                LAST_MODIFIED_ON=`stat --printf %y $FILE | cut -c 1-19`

                FILE_SIZE_BYTES1=`expr $FILE_SIZE_BYTES \* 100`
                PERCENTAGE=`expr $FILE_SIZE_BYTES1 / $DISK_SIZE_BYTES`%

                printf "\n%-18s %-12s %-14s %-25s %s"  "$FILE_SIZE_BYTES" "$PERCENTAGE" "$OWNER" "$LAST_MODIFIED_ON" "$FILE"

                SUM=$(($SUM+$FILE_SIZE_BYTES))
		done

echo
echo

SUM1=`expr $SUM \* 100`
TOTAL_PERCENTAGE=`expr $SUM1 / $DISK_SIZE_BYTES`%

echo "Total disk size:  $DISK_SIZE_BYTES  Bytes"
echo "Total size occupied by these files: $SUM Bytes [ $TOTAL_PERCENTAGE of Total Disc Space  ]"
echo
echo "*** Note: 0% represents less than 1% ***"

echo
